//
//  MeViewController.m
//  Movie
//
//  Created by cjy on 16/4/1.
//  Copyright © 2016年 YJC. All rights reserved.
//

#import "MeViewController.h"
#import <UIImageView+WebCache.h>
#import "LikeViewController.h"
#import "loginVC.h"
#import "DataBaseHandle.h"
#import "PersonViewController.h"
#import "ServiceViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "GuideViewController.h"

@interface MeViewController () <UITableViewDataSource,UITableViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>


@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *array;
@property (nonatomic,strong)NSString *str;
@property (nonatomic, assign)BOOL loginState;

@property (nonatomic,strong)UIButton *photo;
@property (nonatomic,strong)UILabel *name;
@property (nonatomic,strong)NSString *string;

@property (nonatomic,strong)UIImageView *imageBackgroung;

@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"登录" style: UIBarButtonItemStylePlain target:self action:@selector(loginOrExit:)];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target: self action:@selector(presentLeftMenuViewController:)];

    [self viewWillAppear:YES];
    self.navigationItem.title = @"个人中心";
    
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    //设置登录状态
    [self viewWillAppear:YES];
    
    [self handle];
    [self createtableView];
}



- (void)loginOrExit:(UIBarButtonItem *)sender{

        if (![AVUser currentUser] ) {
            //登录
            loginVC *login = [[loginVC alloc] init];
            
            //模态显示
            [self presentViewController:login animated:YES completion:nil];
            

        }else{
            
            //初始化弹窗
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"确认注销" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
                [AVUser logOut];
            
                self.name.text = @"未登录";

                
                //设置登录状态
                [self viewWillAppear:YES];
                
                
            }];
            
            [alert addAction:defaultAction];
            
            [self presentViewController:alert animated:YES completion:^{
                
            }];
        }
}


- (void)viewWillAppear:(BOOL)animated{

    if (![AVUser currentUser]) {
        
        _string = @"登录";
        _name.text = @"未登录";
        _imageBackgroung.image = [UIImage imageNamed:@"1-131030200245Q2"];
        
    }else{
      
        _string = @"注销";
        NSString *currentUsername = [AVUser currentUser].username;// 当前用户名
        _name.text = currentUsername;
    
    }
        self.navigationItem.rightBarButtonItem.title = _string;
 
}

    
 
- (void)handle{

    self.str = [NSString stringWithFormat:@"%0.2fM", ([[SDImageCache sharedImageCache] getSize] / (1024.0 * 1024.0))];
    
    _array = @[
                 @[@{@"name":@""}],
                 @[@{@"name":@"个人资料"},
                   @{@"name":@"收藏"}],
                 @[@{@"name":@"免责声明"},
                   @{@"name":@"清空图片缓存",@"number": self.str},
                   @{@"name":@"新手引导"}]].mutableCopy;
}


- (void)createtableView{
    
    
    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];

    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    _imageBackgroung = [[UIImageView alloc]initWithFrame:CGRectMake(30.f, 45.f, 80.f, 80.f)];
    _imageBackgroung.userInteractionEnabled = YES;
    _imageBackgroung.layer.masksToBounds = YES;
    _imageBackgroung.layer.cornerRadius = 40;
    
    [self.tableView addSubview:_imageBackgroung];

    NSData *data = [[AVUser currentUser] objectForKey:@"touXiang"];
    if (data != nil) {
        
//        self.imageViewOfBack.image = [UIImage imageWithData:data];
        self.imageBackgroung.image = [UIImage imageWithData:data];
    }else{
    
        self.imageBackgroung.image = [UIImage imageNamed:@"1-131030200245Q2"];

    }
    

//
    _name = [[UILabel alloc]initWithFrame:CGRectMake(40, 40, 120, 30)];
    _name.center = CGPointMake(_imageBackgroung.center.x + 40 + 30 + 60, _imageBackgroung.center.y);
    [self.tableView addSubview:_name];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _array.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSArray *arr = _array[section];
    return arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *pool1 = @"resuse";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:pool1];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:pool1] ;
        
    }
    if (indexPath.section == 0) {
    }
    
    cell.selectionStyle = UITableViewCellAccessoryNone;
    NSArray *arr = [_array objectAtIndex:indexPath.section];
    NSDictionary *dic = arr[indexPath.row];
    cell.textLabel.text = [dic objectForKey:@"name"];
    cell.detailTextLabel.text = [dic objectForKey:@"number"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}



//- (void)add{
//
//    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
//    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//    picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
//    picker.allowsEditing = YES;
//    picker.delegate = self;
//    [self presentViewController:picker animated:YES completion:nil];
//    // 将image转成Data类型.
//   
//
//}
//
////上传图片的协议与代理方法
//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
//{
////    UIImage *image=[info objectForKey:UIImagePickerControllerEditedImage];
////    // 将image转成Data类型.
//    NSData *dataHeadImage = UIImagePNGRepresentation(info[UIImagePickerControllerEditedImage]);
//    
//    
//    // 将image添加为用户的属性.并保存.
//    [[AVUser currentUser] setObject:dataHeadImage forKey:@"touXiang"];
//    
//    [[AVUser currentUser] saveInBackground];
////    [self.photo setImage:image forState:UIControlStateNormal];
//    //选取完图片之后关闭视图
//    [self.tableView reloadData];
//    [self dismissViewControllerAnimated:YES completion:nil];
//    
//    
//    
//    
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        if ( ![AVUser currentUser]) {
//            
//            loginVC *login = [[loginVC alloc]init];
////            [self.navigationController pushViewController:login animated:YES];
//            [self presentViewController:<#(nonnull UIViewController *)#> animated:<#(BOOL)#> completion:<#^(void)completion#>]
        }else {
         [AVUser currentUser];
        
        
        }
       
            
    }
    
    else if (indexPath.section == 1 && indexPath.row == 0 ){
        
        PersonViewController *person = [[PersonViewController alloc]init];
        [self.navigationController pushViewController:person animated:YES];
        
        }else if (indexPath.section == 1 && indexPath.row == 1 ){
            
            
            if (![AVUser currentUser]) {
                
                loginVC *login = [[loginVC alloc]init];
           
                [self presentViewController:login animated:YES completion:nil];
                
            }else{
            LikeViewController *like = [[LikeViewController alloc]init];
            
            like.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:like animated:YES];
            }

        }else if (indexPath.section == 2 && indexPath.row == 0 ){
        
            ServiceViewController *service = [[ServiceViewController alloc] init];
            [self.navigationController pushViewController:service animated:YES];
            
        }else if (indexPath.section == 2 && indexPath.row == 1 ){

            [self clear];
        
        }else{
            
            GuideViewController *guide = [[GuideViewController alloc]init];
            [self.navigationController pushViewController:guide animated:YES];

        }
 
    
}





- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        return 100;
    }else{
        
        return 50;
    }
    
}


- (void)clear{
    
    [[SDImageCache sharedImageCache] clearDisk];
    
    self.str = [NSString stringWithFormat:@"%0.2fM", ([[SDImageCache sharedImageCache] getSize] / (1024.0 * 1024.0))];
    
    _array = @[@[@{@"name":@""}],
               @[@{@"name":@"个人资料"},
                 @{@"name":@"收藏"}],
               @[@{@"name":@"免责声明"},
                 @{@"name":@"清空图片缓存",@"number": self.str},
                 @{@"name":@"新手引导"}]].mutableCopy;

    [self.tableView reloadData];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
